package com.luluteam.pushlib.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.luluteam.pushlib.constant.AppHolder;
import com.luluteam.pushlib.constant.CtxConstant;
import com.luluteam.pushlib.tools.ContextInfoManager;
import com.luluteam.pushlib.util.LocationHelper;
import com.luluteam.pushlib.util.ToastUtil;


/**
 * @author Guan
 */
public class LocationService extends Service {

    private static final String TAG = "LocationService";
    LocationHelper locationHelper;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {
        Log.d(TAG, "LocationService onStartCommand()");

        final int action = intent.getIntExtra("action", 0);
        final String jsonTag = intent.getStringExtra("jsonTag");


        locationHelper = LocationHelper.requestLocation(AppHolder.appContext, new LocationHelper.ResponseListener() {
            @Override
            public void onSuccessResponse(double latitude, double longitude) {
                ToastUtil.LOG_TOAST(getApplicationContext(), "纬度=" + latitude + "   longitude=" + longitude);
                if (action == CtxConstant.ACTION_NORMAL) {
                    //单纯的上报情景信息
                    ContextInfoManager.getInstance().postContextInfo(action, latitude, longitude);
                } else if (action == CtxConstant.ACTION_CONTEXT) {
                    //上报情景信息，同时请求服务器进行基于情景上下文的推荐
                    ContextInfoManager.getInstance().postContextInfo(action, jsonTag, latitude, longitude);
                } else if (action == CtxConstant.ACTION_MSG) {
                    //上报情景信息，携带用户行为数据，用做计算评分
                    final String messageId = intent.getStringExtra("messageId");
                    final int rating = intent.getIntExtra("rating", -1);
                    final long duration = intent.getLongExtra("duration", -1);
                    final int wordCount = intent.getIntExtra("wordCount", -1);
                    ContextInfoManager.getInstance().postContextInfo(messageId, rating, duration, wordCount, latitude, longitude);
                }
                stop();
            }

            @Override
            public void onErrorResponse(String provider, int status) {
                ToastUtil.LOG_TOAST(getApplicationContext(), "定位失败");
                if (action == CtxConstant.ACTION_NORMAL) {
                    //单纯的上报情景信息
                    ContextInfoManager.getInstance().postContextInfo(CtxConstant.ACTION_NORMAL, -1, -1);
                } else if (action == CtxConstant.ACTION_CONTEXT) {
                    //上报情景信息，同时请求服务器进行基于情景上下文的推荐
                    ContextInfoManager.getInstance().postContextInfo(CtxConstant.ACTION_CONTEXT, -1, -1);
                } else if (action == CtxConstant.ACTION_MSG) {
                    //上报情景信息，携带用户行为数据，用做计算评分
                    final String messageId = intent.getStringExtra("messageId");
                    final int rating = intent.getIntExtra("rating", -1);
                    final long duration = intent.getLongExtra("duration", -1);
                    final int wordCount = intent.getIntExtra("wordCount", -1);
                    ContextInfoManager.getInstance().postContextInfo(messageId, rating, duration, wordCount, -1, -1);
                }
                stop();
            }
        });


        return super.onStartCommand(intent, flags, startId);
    }

    public void stop() {
        LocationHelper.stopLocation(locationHelper);
        stopSelf();
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "LocationService onDestroy()");
        super.onDestroy();
    }
}
